package com.ifkytek.ninegridview;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.helper.ItemTouchHelper;

import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.internal.entity.CaptureStrategy;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import top.zibin.luban.CompressionPredicate;
import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;

public class ReleaseMessageActivity extends AppCompatActivity implements PictureLayout.PictureLayoutListener{

    private static final int REQUEST_CODE_CHOOSE = 111;
    private final String TAG = "ReleaseMessage_log";
    private ItemTouchHelper itemTouchHelper;
    private ArrayList<ImageModel> datas;
    private PictureLayout pictureLayout;
    public static final String LOCAL_CACHE_PATH = Environment.getExternalStorageDirectory().getPath() + "/iflytek_aimc";
    public static void startActivity(Context context){
        Intent intent = new Intent();
        intent.setClass(context,ReleaseMessageActivity.class);
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_releasemessage);
        datas = new ArrayList<>();
        pictureLayout = (PictureLayout) findViewById(R.id.pic_layout);
        pictureLayout.setPictureLayoutListener(this);
        ImageModel imageModel = new ImageModel();
        imageModel.setLastImage(true);
        datas.add(imageModel);
    }

    @Override
    public void choosePicture() {
        Matisse.from(ReleaseMessageActivity.this)
                .choose(MimeType.ofAll())
                .countable(false)
                .maxSelectable(1)
                .originalEnable(true)
                .captureStrategy(new CaptureStrategy(true,"com.ifkytek.ninegridview.fileprovider"))
                .maxOriginalSize(10)
                .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
                .thumbnailScale(0.85f)
                .imageEngine(new Glide4Engine())
                .forResult(REQUEST_CODE_CHOOSE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_CODE_CHOOSE&&resultCode== RESULT_OK){
            List<Uri> uris = Matisse.obtainResult(data);
            pictureLayout.addMessage(uris.get(0).toString());
        }

//        Luban.with(ReleaseMessageActivity.this)
//                .load(uris.get(0))
//                .ignoreBy(10)
//                .setTargetDir(LOCAL_CACHE_PATH)
//                .setCompressListener(new OnCompressListener() {
//                    @Override
//                    public void onStart() {
//
//                    }
//
//                    @Override
//                    public void onSuccess(File file) {
//                        pictureLayout.addMessage(file.getAbsolutePath());
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//
//                    }
//                }).launch();

    }
}